<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0 " @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- 实现新增部门或者编辑部门的对话框 -->
        <el-dialog
          :title="isEdit?'编辑部门':'新增子部门'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 父传子  传id  v-if="showDialog" 销毁dom更新表单数据项  @close="showDialog = false" 子传父自定义事-->
          <addOrEdit
            v-if="showDialog"
            :id="curId"
            :is-edit="isEdit"
            :origin-list="originList"
            @close="showDialog = false"
            @success="hSuccess"
          />
        </el-dialog>

      </el-card>

    </div>
  </div>
</template>

<script>
// 按需导入封装好的请求函数
import { getDepartments, delDepartments } from '@/api/departments'
// 树形结构数据的函数
import { tranDataToTree } from '@/utils'
// 导入子组件
import addOrEdit from './deptDialog.vue'
export default {
  name: 'VueHrDepartments',
  components: {
    // 注册子组件
    addOrEdit
  },
  data() {
    return {
      // 设置对话框的显示和隐藏
      showDialog: false,
      list: [], // 保存当前部门列表
      curId: '', // 保存当前部门的id
      isEdit: false, // false新增子部门, true 编辑部门
      originList: [] // 存储所有部门数据的部分的部分数据(id,pid,code)
    }
  },
  // 页面一加载就触发的钩子
  created() {
    this.loadDepartments()
  },
  methods: {
    // 删除按钮
    hDel(id) {
      // 根据id删除部门
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartments(id)
        // 关闭弹窗
        this.showDialog = false
        // 重新渲染页面
        this.loadDepartments()
      })
    },
    // 自定义事件关闭弹窗,渲染页面
    hSuccess() {
      // 渲染页面
      this.loadDepartments()
      // 关闭弹窗
      this.showDialog = false
    },
    async loadDepartments() {
      const res = await getDepartments()
      // 防止发送请求出错
      try {
        // 获取部门数据并取出,数组中每个对象的部门数据(id,pid,code)
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        // console.log(res)
        // 删除数组里面第一个公司的名称
        res.data.depts.shift()
        this.list = tranDataToTree(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 新增部门
    hAdd(id) {
      this.curId = id
      // 显示弹窗
      this.showDialog = true
      this.isEdit = false
    },
    // 编辑部门
    hEdit(id) {
      this.isEdit = true
      this.showDialog = true
      this.curId = id
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
